import React, { Component } from 'react'
import ReactDOM from 'react-dom';
import { Modal, Form } from 'antd';
import { ModalForm } from '../../../components'
import { fetchData } from '../../../components/AjaxConsumer'
import store from '../../../stores'
 
const confirm = Modal.confirm;
 
const WrappedAdvancedSearchForm = Form.create()(ModalForm);
 
export default class UpdateGroupPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            visible: false,
            resData: null,
            submitUrl:'/base/factory/group/c'
        };
        //和stroes进行通信挂钩 并绑定解绑事件unsubscribe
        this.unsubscribe = store.listen((resData)=>{
            this.setState({
                resData: resData
            })
        })
 
        this.formAlerSize = 'small'; //弹出框的大小 只有'large'和'small'两种值 （必须,输错时默认为large）
        this.getFormData = [
            {
                type : 'hiddenInput',
                showName : 'factoryGroupId',
                name : 'factoryGroupId',
                initialValue : ''
            },
            {
                type : 'input',
                showName : '工厂组编码',
                name : 'groupCode',
                initialValue : '',
                required:true  //选填
            },{
                type : 'input',
                showName : '工厂组名称',
                name : 'groupName',
                initialValue : '',
                required:true
            },{
                type : 'input',
                showName : '备注',
                name : 'remark',
                initialValue : '',
                //required:'required'
            }
        ];
    }
    //组件销毁时解绑与stores的监听
    componentWillUnmount(){
        this.unsubscribe();
    }
    //显示弹框
    showModal = () => {
        this.setState({
            visible: true,
        });
    }
    // 隐藏弹出框
    hideModal = () => {
        this.setState({
            visible: false,
        });
    }
    // 新增工作区
    addHandle = () => {
        this.setState(
            {
                submitUrl:'/base/factory/group/c',
                formTitle: '新增工厂组'
            }
        );
        //清空input值
        this.getFormData.map( (e, index) => {
            e.initialValue = ''
        });
        this.setState({resData: null});
        this.showModal();//显示弹框
    }
    //11111
    editGetInfoCallBack11 = (resData) =>{
        let tmpFormData = this.state.formData;
        // 编辑时候渲染初始数据
        // 1 先加载下拉框
        let factoryCode = 'all';
        let options= {
            url: '/base/factory/group/'+factoryCode+'/group',
            type: 'post',
            condition:{}
        };
        // 当前item塞入resData
        this.setState({resData:resData});
        fetchData(this, options, this.queryWarehousesUnderFactoryEditCallBackHandle);
 
    }
 
    editGetInfoCallBack = (resData) =>{
        this.setState({
            submitUrl:'/base/factory/group/update',
            resData: resData
        });
        this.showModal();//显示弹框
    }
 
        // 编辑信息
        editHandle = (rowObj) => {
            this.setState({resData: null});
            //根据对象的Id 属性获取对象信息
            let factoryGroupId = rowObj.factoryGroupId;
            const options= {
                url: '/base/factory/group/'+factoryGroupId,
                type: 'get',//请求方式
                condition:{}
            };
            fetchData(this, options, this.editGetInfoCallBack);
        }
    
//     // 编辑信息
//     editHandle = (rowObj) => {
//         this.setState({
//             formTitle: '修改工厂组'
//         });
//         //根据对象的Id 属性获取对象信息
//  //       let factoryGroupId = rowObj.factoryGroupId;
//         const options= {
//             url: '/base/factory/group/'+rowObj.factoryGroupId,
//             type: 'get',//请求方式
//             condition:{}
//         };
//         fetchData(this, options, (resData) => {
//             this.setState({
//                 resData: resData
//             });
 
//             this.showModal();//显示弹框
//         });
//     }
    // 删除后的回调
    deleteCallBackHandle = ()=>{
        let result = this.state.resData;
        if (result.code == '1'){
            message.success('删除成功！');
            this.refreshHandle();
        }else {
            message.info(result.msg);
        }
    }
 
    // 删除工作区
    deleteHandle = (rowObj) => {
        let factoryGroupId = rowObj.factoryGroupId;
        // let userIdArr = [];
        // userIdArr.push(userId);
        const options= {
            url: '/base/factory/group/del',
            condition:{factoryGroupId:factoryGroupId}
        };
        let _that = this;
        confirm({
            title: '您确定要删除吗？',
            onOk() {
                fetchData(_that, options, _that.deleteCallBackHandle);
                console.log("------------删除操作------------------")
            },
            onCancel() {
                console.log("取消了删除操作")
            },
        });
    }
 
    // 新增/修改后的回调
    handleOk = () => {
        // let result = this.state.resData;
        this.hideModal();
        this.refreshHandle();
    }
 
 
    handleCancel = () => {
        //表单重置
       this.refs.modalFrom.resetFields();
       this.hideModal();
    }
 
    //刷新List列表
    refreshHandle = () => {
        console.log("刷新List列表")
        this.props.refreshHandle();
    }
 
 
 
    render() {
        if(this.state.resData != null &&  this.state.resData.data != null){
            let factory = this.state.resData.data;
            this.getFormData.map( (e, index) => {
                e.initialValue = factory[e.name]
            });
        }
 
        return(
            <Modal
                style={{top:'20px'}}
                visible={this.state.visible}
                title={this.state.formTitle ? this.state.formTitle : '填写工厂组'}
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                width={this.formAlerSize=='large'?'850px':'400px'}
                footer=''
            >
 
                <WrappedAdvancedSearchForm
                    ref="modalFrom"
                    title="工厂组信息"
                    formAlerSize={this.formAlerSize}
                    formUrl={this.state.submitUrl}
                    getFormData={this.getFormData}
                    closeBox={this.handleCancel}
                    checkBox={this.handleOk} />
 
            </Modal>
        )
    }
}